/*
   Project AwesOmeScript
   Copyright 2010 Domenico Gentner

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 2 of the License, or
   (at your option) any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include "stdafx.hpp"
#include "IconBrowser.hpp"
#include "Mainframe.hpp"

//wxLC_ICON
const long ASS_LIST_CTRL_STYLE = wxLC_SMALL_ICON|wxLC_SINGLE_SEL|wxLC_NO_HEADER;

wxString wxSizeToString(const wxSize& size)
{
	wxString s;
	s << size.GetX() << "x" << size.GetY();
	return s;
}

wxSize wxStringToSize(const wxString& str)
{
	wxSize s;
	int x = 0; int y = 0;

	std::wstringstream ss;
	ss << str.BeforeFirst('x');
	ss >> x;
	ss.clear();

	ss << str.AfterFirst('x');
	ss >> y;

	return wxSize(x,y);
}


BEGIN_EVENT_TABLE(IconBrowser, wxFrame)
	EVT_CLOSE(IconBrowser::OnMyClose)
	EVT_LIST_ITEM_ACTIVATED(wxID_ANY, IconBrowser::OnListItemDblClicked)
END_EVENT_TABLE()


IconBrowser::IconBrowser(Mainframe* parent)
: wxFrame(parent, wxID_ANY, _("Select the icon to insert..."), wxDefaultPosition, wxSize(300,400)),
  m_selected_icon(-1), m_notebook(NULL), m_mainframe(parent)
{
	wxArrayString all_dirs;

	// Prevent the warnings about the not proper png
	wxLogNull noLog;

	// Create Notebook
	m_notebook = new wxNotebook(this, wxID_ANY);

	// Get all directories
	MyDirTraverser traverser(all_dirs);
	wxDir dir("icons");

	if ( dir.IsOpened() )
	{
		dir.Traverse(traverser);

		for ( size_t i = 0; i < all_dirs.GetCount(); i++ )
		//for ( size_t i = 0; i < 1; i++ )
		{
			// Create new ListControl
			wxListView* lstCtrl = new wxListView(m_notebook, wxID_ANY, wxDefaultPosition, wxDefaultSize, ASS_LIST_CTRL_STYLE);	
			wxFileName fn(all_dirs[i]);
			m_notebook->AddPage(lstCtrl, fn.GetName());


			// Show Progress
			wxProgressDialog * progress = new wxProgressDialog (_("Please wait..."), _("Searching for Images.."), 1, this);

			// Get Files
			wxArrayString files;
			wxDir::GetAllFiles(all_dirs[i], &files, "*.png", wxDIR_FILES);

			// Create ImageList
			wxSize size = wxStringToSize(fn.GetName());
			wxImageList* images = new wxImageList(size.x, size.y, false, files.GetCount());

			// Update Progress
			progress->Update(1, _("Found all files.."));
			wxDELETE(progress);

			// New Dialog
			progress = new wxProgressDialog (_("Please wait..."), _("Adding Images.."), files.GetCount(), this);

			// Traversing files
			for ( size_t i = 0; i < files.GetCount(); i++ )
			{

				// Getting the file		
				wxFileName f_source(files.Item(i));
				f_source.MakeAbsolute();

				// Updating progress
				wxString msg;
				msg << _("Processing: ") << i << "/" << files.GetCount() << ": " << f_source.GetFullName();
				progress->Update(i, msg);

				// Add into lists
				wxString s_display = files.Item(i);
				s_display.Replace("\\", "/");
				lstCtrl->InsertItem(i, s_display, i);
				images->Add(wxBitmap(f_source.GetFullPath(), wxBITMAP_TYPE_PNG));
			}

			lstCtrl->AssignImageList(images, wxIMAGE_LIST_SMALL);
			wxDELETE(progress);
		}
	}
	else
	{
		wxMessageBox(_("Icons have not been imported yet!"), wxMessageBoxCaptionStr, wxICON_ERROR|wxOK, this);
	}
}

//////////////////////////////////////////////////////////////////////////
// Reads the output from the db-extractor and sorts the images for size
void IconBrowser::DoSortImages( wxWindow* parent, const wxString& path, const wxString& to )
{
	// Prevent the warnings about the not proper png
	wxLogNull noLog;

	// Show Progress
	wxProgressDialog * progress = new wxProgressDialog (_("Please wait..."), _("Searching for Images.."), 1, parent);

	// Get Files
	wxArrayString* files = new wxArrayString;
	wxDir::GetAllFiles(path, files, "*.png", wxDIR_FILES);

	// Update Progress
	progress->Update(1, _("Found all files.."));
	wxDELETE(progress);

	// New Dialog
	progress = new wxProgressDialog (_("Please wait..."), _("Copying Images.."), files->GetCount(), parent);

	// Check Directory
	if ( !wxDirExists(to))
	{
		wxMkDir(to);
	}

	// Traversing files
	for ( size_t i = 0; i < files->GetCount(); i++ )
	{
		// Getting the file		
		wxFileName f_source(files->Item(i));
		f_source.MakeAbsolute();

		// Updating progress
		wxString msg;
		msg << _("Processing: ") << i << "/" << files->GetCount() << " :" << f_source.GetFullName();
		progress->Update(i, msg);
		
		// Read into wxImage
		wxImage img(f_source.GetFullPath(), wxBITMAP_TYPE_PNG);
		wxSize size = img.GetSize();

		// we omit all images with size 0,0 since this may be an error.
		if ( size != wxSize(0,0) )
		{
			// Target
			wxFileName f_target(to + "/" + wxSizeToString(size) + "/" + f_source.GetFullName());
			f_target.MakeAbsolute();
			if ( !wxDirExists( f_target.GetPath()) )
			{
				wxMkDir( f_target.GetPath() );
			}
			wxCopyFile(f_source.GetFullPath(), f_target.GetFullPath(), false);
		}
	}

	wxDELETE(progress);
	wxDELETE(files);
}

void IconBrowser::OnMyClose( wxCloseEvent& e )
{	
	if ( e.CanVeto() )
	{
		this->Hide();
		e.Veto();
	}	
	else
	{
		e.Skip();
	}
}	

void IconBrowser::OnListItemDblClicked( wxListEvent& e )
{
	m_mainframe->InsertIcon("<img src=" + e.GetItem().GetText() + ">");
}